<template>
  <el-dialog title="预约信息" :visible.sync="visible" width="60%">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      label-width="110px"
    >
      <h3>个人信息</h3>
      <el-form-item label="宠物主人" prop="nickName">
        <el-input v-model="dataForm.nickName" disabled clearable placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="宠物名称" prop="petId">
        <el-select
          v-model="dataForm.petId"
          @change="changePet"
          clearable
          placeholder="请选择"
          style="width: 100%;"
        >
          <el-option v-for="item in petOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="dataForm.phone" clearable placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="住址" prop="address">
        <el-input v-model="dataForm.address" clearable placeholder="请输入"></el-input>
      </el-form-item>
      <h3>预约信息</h3>
      <el-form-item label="预约日期" prop="time">
        <el-date-picker
          v-model="dataForm.time"
          @change="changeTimer"
          type="date"
          value-format="yyyy-MM-dd"
          clearable
          placeholder="选择日期"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="预约时间" prop="flag">
        <el-select
          v-model="dataForm.flag"
          @change="changeTimer"
          clearable
          placeholder="请选择"
          style="width: 100%;"
        >
          <el-option
            v-for="item in timeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预约医生" prop="medicId">
        <el-select
          v-model="dataForm.medicId"
          @change="changeMedic"
          placeholder="请选择"
          style="width: 100%;"
        >
          <el-option v-for="item in careOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer" v-if="!dataForm.status && roleList[0].roleId != '3'">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDataInfo, savaDataInfo } from "@/api/apply";
import { getCareList } from "@/api/user";
import { getDataList } from "@/api/pet";
import { getZhiBanYiSheng } from "@/api/hospital/working";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      visible: false,
      dataForm: {},
      dataRule: {},
      careOptions: [],
      allCareOptions: [],
      timeOptions: [
        { label: "早上(8:00 - 16:00)", value: "zaoBan" },
        { label: "下午(16:00 - 24:00)", value: "zhongBan" },
        { label: "晚上(00:00 - 8:00)", value: "wanBan" }
      ],
      petOptions: [],
      
    };
  },
  computed: {
    ...mapGetters(["userInfo", "roleList"])
  },
  created() {
    this.getPetList();
  },
  methods: {

    init(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.dataForm = {
          id,
          userId: this.userInfo.id,
          nickName: this.userInfo.name,
          status: 0
        };
 
        if (id) {
          getDataInfo(id)
            .then(data => {
              if (data && data.code === 0) {
                this.dataForm = data.data;
                this.changeTimer();
              }
            })
            .catch(err => {
              console.log(err);
            });
        }
      });
    },
    // 获取下拉框
    getPetList() {
      // 获取宠物下拉框
      getDataList({ limit: 999, userId: this.userInfo.id })
        .then(res => {
          this.petOptions = res.page.list;
        })
        .catch(() => {});
      // 获取医护人员
      getCareList()
        .then(res => {
          if (res && res.code === 0) {
            this.allCareOptions = res.data;
          }
        })
        .catch(() => {});
    },
    // 修改宠物
    changePet(val) {
      if (val) {
        const obj = this.petOptions.find(item => item.id === val);
        this.dataForm.petName = obj.name;
      } else {
        this.dataForm.petName = "";
      }
    },
    // 修改时间段，修改可选择的医生
    changeTimer() {
      this.careOptions = [];
      if (this.dataForm.flag && this.dataForm.time) {
        getZhiBanYiSheng({ flag: this.dataForm.flag, time: this.dataForm.time })
          .then(res => {
            if (res.data) {
              const data = res.data.split(",");
              this.careOptions = this.allCareOptions.filter(item => {
                return data.includes(item.id);
              });
            }
            console.log(this.careOptions);
          })
          .catch(() => {});
      }
    },
    changeMedic(val) {
      let obj = this.careOptions.find(item => item.id === val);
      this.dataForm.medicName = obj.name;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          savaDataInfo(this.dataForm)
            .then(res => {
              if (res && res.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              }
            })
            .catch(err => {
              console.log(err);
            });
        }
      });
    }
  }
};
</script>
